package com.navinfo.platform.fault.service.day;

import com.navinfo.platform.fault.utils.BigDataUtils;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.types.StructType;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public enum BasicDataService {

    INS;

    public static void saveData(SparkSession spark, Dataset<Row> saveDataset, Map<String, String> configMap, String tableNameKey, String day, StructType schema) {
        String tableName = configMap.get(tableNameKey);
        String saveHiveDbName = configMap.get("saveHiveDbName");
        List<String> partationKeys = Collections.singletonList("day");
        List<String> columns = getSelectColumns(schema);
        List<Object> partationValues = Collections.singletonList(day);
        BigDataUtils.saveDataSetToHiveTable(spark, saveDataset, BigDataUtils.CoalesceType.repartation, 1, saveHiveDbName, tableName, partationKeys, partationValues, columns);
    }

    static List<String> getSelectColumns(StructType schema) {
        String[] fieldNames = schema.fieldNames();
        return Arrays.asList(fieldNames);
    }
}
